package com.example.asset.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.asset.entity.AssetInformation;
import com.example.asset.entity.vo.KeyVal;
import com.example.asset.mapper.AssetInformationMapper;
import com.example.asset.utils.UtilRandom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class AssetInformationService extends ServiceImpl<AssetInformationMapper, AssetInformation>{

    @Autowired
    private AssetInformationMapper assetInformationMapper;

    /**
     * 保存添加的资产信息
     *
     * @param assetInformation
     * @return
     */
    public boolean saveAssetInformation(AssetInformation assetInformation){
        // 添加资产信息
        assetInformation.setAssetNumber(generateUniqueSerialNumber());
        assetInformation.setAddtime(new Date());
        return saveOrUpdate(assetInformation);
    }

    /**
     * 生成生成资产编号
     *
     * @return
     */
    public String generateUniqueSerialNumber(){
        String assetNum;
        boolean isUnique = false;

        Pattern pattern = Pattern.compile("\\d{14}");
        Matcher matcher;

        do{
            // 生成一个14位的随机数
            assetNum = UtilRandom.generateRandomNumber();
            // 匹配正则表达式
            matcher = pattern.matcher(assetNum);
            // 如果数据库中没有相同的资产编号，流水号是唯一的
            if(matcher.matches() && QuerryAssetNumber(assetNum) == null){
                isUnique = true;
            }
        } while(!isUnique);

        return assetNum;
    }

    /**
     * 检查数据库是否存在相同的资产编号
     *
     * @param assetNumber
     * @return
     */
    public AssetInformation QuerryAssetNumber(String assetNumber){
        QueryWrapper<AssetInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("asset_number", assetNumber);
        return assetInformationMapper.selectOne(queryWrapper);
    }


    /**
     * 用于更新资产状态
     *
     * @param assetNumber
     * @param state
     * @return
     */
    public boolean UpdateAssetState(String assetNumber, String state){
        QueryWrapper<AssetInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("asset_number", assetNumber);
        AssetInformation assetInformation = assetInformationMapper.selectOne(queryWrapper);
        assetInformation.setAssetState(state);
        assetInformation.setUeser("");
        return saveOrUpdate(assetInformation);
    }


    /**
     * 根据资产编号查找指定资产信息
     *
     * @param assetNum
     * @return
     */
    public AssetInformation selectAssetNum(String assetNum){
        QueryWrapper<AssetInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("asset_number", assetNum);
        return assetInformationMapper.selectOne(queryWrapper);
    }

    public double AssetMoney(){
        Double tmp = assetInformationMapper.assetMoney();
        return tmp;

    }

    public Long AssetCount(){
        QueryWrapper<AssetInformation> queryWrapper = new QueryWrapper<>();
        return assetInformationMapper.selectCount(queryWrapper);

    }

    public List<KeyVal> listState(){
        return this.baseMapper.listState();
    }
}